package cn.lixu.searchMaster.controller;

import cn.lixu.basic.annotation.PreAuthorize;
import cn.lixu.basic.exception.BusinessException;
import cn.lixu.basic.util.JsonResult;
import cn.lixu.basic.util.LoginContext;
import cn.lixu.basic.util.PageList;
import cn.lixu.org.domain.Logininfo;
import cn.lixu.searchMaster.domain.SearchMasterMsg;
import cn.lixu.searchMaster.dto.AccpetDto;
import cn.lixu.searchMaster.query.SearchMasterMsgQuery;
import cn.lixu.searchMaster.service.ISearchMasterMsgService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部")
    @PreAuthorize(name = "查询全部寻主列表", sn = "searchMasterMsg:list") //value后面用来做权限校验
    public List<SearchMasterMsg> loadAll(HttpServletRequest request) {
        //获取登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return searchMasterMsgService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一个")
    @PreAuthorize(name = "查询一个寻主列表", sn = "searchMasterMsg:loadById") //value后面用来做权限校验
    public SearchMasterMsg loadById(@PathVariable("id") Long id) {
        return searchMasterMsgService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.remove(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param searchMasterMsg 添加和修改
     * @return
     */
    @PutMapping
    public JsonResult addAndUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() == null) {
                searchMasterMsgService.save(searchMasterMsg);
            } else {
                searchMasterMsgService.update(searchMasterMsg);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param searchMasterMsgQuery
     * @return
     */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery) {
        return searchMasterMsgService.queryPage(searchMasterMsgQuery);

    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            searchMasterMsgService.patchDel(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 寻主接口
     *
     * @param smm
     * @param request
     * @return
     */
    @PostMapping("/publish")
    @ApiOperation(value = "寻主接口")
    public JsonResult publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request) {
        try {
            //获取logininfo对象
            Logininfo logininfo = LoginContext.getLogininfo(request);
            searchMasterMsgService.publish(smm, logininfo.getId());
            return JsonResult.me();
        } catch (BusinessException e) {
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            return JsonResult.me().setMsg("网络异常");
        }
    }

    /**
     * 待审核
     *
     * @param searchMasterMsgQuery
     * @return
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery) {
        return searchMasterMsgService.queryPage(searchMasterMsgQuery);

    }


    /**
     * 审核待处理接口
     *
     * @param searchMasterMsgQuery
     * @return
     */
    @ApiOperation(value = "待处理查询接口")
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            return searchMasterMsgService.toHandle(searchMasterMsgQuery, logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 用户处理接口
     *
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @ApiOperation(value = "用户处理接口")
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        try {

            Logininfo logininfo = LoginContext.getLogininfo(request);
            return searchMasterMsgService.user(searchMasterMsgQuery, logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 拒单
     *
     * @param id
     * @return
     */
    @GetMapping("/reject/{id}")
    @ApiOperation(value = "拒单")
    public JsonResult reject(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.reject(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常，稍后重试");
        }
    }

    /**
     * 接单
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/accept")
    @ApiOperation(value = "接单")
    public JsonResult accept(@RequestBody AccpetDto dto, HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            searchMasterMsgService.accept(dto,logininfo.getId());
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("订单正在处理中，请勿再次处理");
        }
    }

}

